--
-- Author: shunguo.chen
-- Date: 2019-05-23 17:26:05
--
-- SkillUpBtnPanel 技能升级按钮panel
--

local ClassRef = app.mvc.createPanelClass(...)

--data = Skill
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.Button:addClickEventListener(function()
        executeFunc(self._onClick, self._data)
    end)

end

-- data = Skill 
function ClassRef:refresh()
    if self._data then
        local skill = self._data
        local isLearned = skill:isLearned()
        local canGet = skill:canGet()
        local canUpgrade = skill:canUpgrade()
        self._ui.Text_name:setString(isLearned and L("text_10300009") or L("text_11700003"))

        local minLv, costNum
        if isLearned then
            minLv, costNum = skill:getNextSkillLvParams()
        else
            minLv = skill:getMinLv()
            costNum = skill:getCostNum()
        end

        local hasSkillPoint = UD:getSkillPoint() >= costNum
        display.setBtnTouchEnable(self._ui.Button, hasSkillPoint and canUpgrade)
        if canUpgrade then
            self._ui.bg:setVisible(true)
            self._ui.icon:setVisible(true)
            self._ui.num:setVisible(true)
            self._ui.tips:setVisible(false)

            self._ui.num:setTextColor(hasSkillPoint and display.COLOR_WHITE or cc.c3b(255, 105, 85))
            self._ui.num:setString(costNum)
        else
            self._ui.bg:setVisible(false)
            self._ui.icon:setVisible(false)
            self._ui.num:setVisible(false)
            self._ui.tips:setVisible(true)
            
            self._ui.tips:setString(L("skillUpgradeButtontips") .. minLv)
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
